package question;

/**
 * 问题3：打印奇数数字
 *
 * @author 阳秋豪
 * @描述 同时开启两个线程，共同获取1-100之间的所有数字要求:将输出所有的奇数，
 * @createTime 2024/3/12
 */
public class Test3 {
    public static void main(String[] args) {
        QOneRunnable3 runnable = new QOneRunnable3();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.setName("A");
        t2.setName("B");
        t1.start();
        t2.start();
    }
}

class QOneRunnable3 implements Runnable {
    int num = 1;

    @Override
    public void run() {
        while (true) {
            synchronized (QOneRunnable3.class) {
                if (num > 100) {
                    break;
                } else {
                    System.out.println(Thread.currentThread().getName() + num);
                    num += 2;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}